﻿var world;
var selectedTile = null;
function getWorldTile(row, col) {
    return world.tiles[row].row[col];
}
var page = "Minesweeper.aspx";
//var page = "working.htm";
$(document).ready(function () {
    //alert("Loading...");
    $.get(page, function (data) {
        try {
            world = eval("(" + data + ")");
            //alert(world);
            loadTiles(world);
            displayActions();
        } catch (e) { alert(e); }
    });
});
function tileClicked(tileTD, row, col) {
    var tile = getWorldTile(row, col);
    // clear the current selection
    var doNotSelect = false;
    if (selectedTile != null) {
        if((selectedTile[0]==row)&&(selectedTile[1]==col)){
            doNotSelect= true;
        }
        document.getElementById("tileSquare-" + selectedTile[0] + "-" + selectedTile[1]).style.borderColor = (tile.actions!=null)?'green':'black';
        selectedTile = null;
    }
    if ((tile.actions != null) && (doNotSelect!=true)) {
        document.getElementById("tileSquare-" + row + "-" + col).style.borderColor = "yellow";
        selectedTile = [row, col];
    }
    displayActions();
}
function displayActions() {
    try {
        var html = "";
        if (world.actions != null) {
            for (var i = 0; i < world.actions.length; i++) {
                html += "<input type='button' onclick='performAction(\"A\", " + i + ")' value='" + world.actions[i].display + "' /><br/>";
            }
        }
        if (selectedTile != null) {
            html += "Selected tile: " + selectedTile[0] + ", " + selectedTile[1] + "<br/>";
            var actions = getWorldTile(selectedTile[0], selectedTile[1]).actions;
            if (actions != null) {
                for (var i = 0; i < actions.length; i++) {
                    html += "<input type='button' onclick='performAction(\"T\", " + i + ", " + selectedTile[0] + ", " + selectedTile[1] + ")' value='" + actions[i].display + "' /><br/>";
                }
            }
        }
        document.getElementById("gameStuffContainerTD").innerHTML = html;
    } catch (e) { alert(e); }
}
function performAction(actionType, actionId, selectedRow, selectedCol) {
    var action = null;
    if (actionType == "A") {
        action = world.actions[actionId];
    } else if (actionType == "T") {
        var tile = getWorldTile(selectedRow, selectedCol);
        action = tile.actions[actionId];
    }
    $.get((page=="working.htm")?("working"+action.id+".htm"):(page + "?" + action.id), function (data) {
        try {
            world = eval("(" + data + ")");
            //alert(world);
            selectedTile = null;
            loadTiles(world);
            displayActions();
        } catch (e) { alert(e); }
    });
}
function writeDebug(source, msg, level) {
    if (level == 0) {
        //chatContainerDiv.innerHTML += source + "(" + msg + ")<br/>";
    }
}
function loadTiles(world) {
    //tileContainerOuterDiv
    var h = world.tiles.length * 54;
    var w = world.tiles[0].row.length * 54;
    document.getElementById("tileContainerOuterDiv").width = w;
    var html = '<table class="tileContainerTable" cellspacing="0" cellpadding="0" height="' + (h) + '" width="' + (w) + '">';
    writeDebug("world.id", world.id, 0);
    writeDebug("world.tiles.length", world.tiles.length, 0);
    for (var i = 0; i < world.tiles.length; i++) {
        writeDebug("adding row ", i, 0);
        html += '<tr class="tileRow" id="tileRow-' + i + '">';
        writeDebug("world.tiles[i].row.length", world.tiles[i].row.length, 0);
        for (var ii = 0; ii < world.tiles[i].row.length; ii++) {
            var tile = world.tiles[i].row[ii];
            writeDebug("", "adding tile " + ii + " to row " + i, 0);
            html += '<td class="tileSquare" id="tileSquare-' + i + '-' + ii + '" onclick="tileClicked(this, ' + i + ', ' + ii + ')" style="border-color:' + ((tile.actions!=null)?'green':'black') + ';background-image:url(\'images/' + tile.background + '.png\')">';
            html += '<img class="tileSquareImg" id="tileSquareImg-' + i + '-' + ii + '" src="images/' + tile.image + '.png" />';
            html += '</td>';
        }
        html += '<td width="*"></td></tr>';
    }
    html += '</table>';
    //writeDebug("html", html, 0);
    document.getElementById("tileContainerOuterDiv").innerHTML = html;
}